package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

//递归:4ms 90%,4MB 84%
func buildTree(preorder []int, inorder []int) *TreeNode {
	if len(preorder) == 0 {
		return nil
	}
	res := &TreeNode{
		Val: preorder[0],
	}
	for i := 0; i < len(preorder); i++ {
		if inorder[i] == preorder[0] {
			res.Left = buildTree(preorder[1:i+1], inorder[:i])
			res.Right = buildTree(preorder[i+1:], inorder[i+1:])
			break
		}
	}
	return res
}

func main() {
	fmt.Println(buildTree([]int{1}, []int{1}))
}
